package com.seafwg.controller;

import com.seafwg.domain.User;
import com.seafwg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author seafwg
 * @Date 2022/3/4 3:21 下午
 * @Version 1.0
 * @description xxx
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    // 登陆
    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session) {
        // 调用userService查询，查询出来保存到session域对象中
        User user = userService.login(username, password);
        if(user != null) { // 用户不为空用户信息保存到session中
            session.setAttribute("user", user);
            return "redirect:/index.jsp";
        }

        return "redirect:/login.jsp";
    }

    @RequestMapping("/findAll")
    public ModelAndView findAll() {
        List<User> userList = userService.findAll();

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("userList", userList);
        modelAndView.setViewName("user-list");

        return modelAndView;
    }
}
